<!---creates a new user account--->
<cfquery name = "qryCheckEmail" datasource="#request.dsn#">
SELECT * FROM core_accounts
WHERE email = '#form.email#'
</cfquery>

<cfset errormsg = "">
<cfset waserror = 0>

<cfset new_nick = replace(form.username, " ", "", "ALL")>
<cfset new_nick = replace(new_nick, "-", "", "ALL")>
<cfset new_nick = replace(new_nick, "*", "", "ALL")>
<cfset new_nick = replace(new_nick, "/", "", "ALL")>
<cfset new_nick = replace(new_nick, "\", "", "ALL")>
<cfset new_nick = replace(new_nick, ",", "", "ALL")>
<cfset new_nick = replace(new_nick, "'", "", "ALL")>
<cfset new_nick = replace(new_nick, "&", "and", "ALL")>
<cfset new_nick = replace(new_nick, "!", "", "ALL")>
<cfset new_nick = replace(new_nick, "(", "", "ALL")>
<cfset new_nick = replace(new_nick, ")", "", "ALL")>
<cfset new_nick = replace(new_nick, "+", "", "ALL")>
<cfset new_nick = replace(new_nick, "=", "", "ALL")>
<cfset new_nick = replace(new_nick, "##", "", "ALL")>
<cfset new_nick = replace(new_nick, "$", "", "ALL")>
<cfset new_nick = replace(new_nick, "%", "", "ALL")>
<cfset new_nick = replace(new_nick, "^", "", "ALL")>
<cfset new_nick = replace(new_nick, "@", "", "ALL")>

<cfif qryCheckEmail.recordcount GT 0>
	<cfset waserror = 1>
    <cfset errormsg = "#errormsg#The email address you used was already used to create an account.">
</cfif>

<cfset strCaptcha = Decrypt(form.captcha_check, request.seedstring, "CFMX_COMPAT","HEX") />

<cfinvoke component="cfcs.common.captcha" method="compareit" textstring="#form.vcode#" encstring="#form.captcha_check#" returnvariable="captcha_passed">

<cfif captcha_passed IS 0>
	<cfset waserror = 1>
    <cfset errormsg = "#errormsg#The characters you entered for the verification image were incorrect.">
</cfif>

<cfquery name = "qDuplicate" datasource="#request.dsn#">
SELECT * FROM core_accounts
WHERE username = '#new_nick#'
</cfquery>

<cfif qDuplicate.recordcount GT 0>
	<cfset waserror = 1>
    <cfset errormsg = "The Nickname you chose is already taken.  Please change it to something else.<br>">
</cfif>

<cfif len(trim(new_nick)) LT 3>
	<cfset waserror = 1>
    <cfset errormsg = "#errormsg#Nickname must be at least 3 characters.<br>">
</cfif>

<cfif len(trim(form.firstname)) IS 0>
	<cfset waserror = 1>
    <cfset errormsg = "#errormsg#A first name is required.<br>">
</cfif>

<cfif len(trim(form.lastname)) IS 0>
	<cfset waserror = 1>
    <cfset errormsg = "#errormsg#A last name is required.<br>">
</cfif>

<cfif len(trim(form.email)) IS 0>
	<cfset waserror = 1>
    <cfset errormsg = "#errormsg#A first name is required.<br>">
</cfif>

<cfif len(trim(form.password)) IS 0>
	<cfset waserror = 1>
    <cfset errormsg = "#errormsg#Please enter a password.<br>">
</cfif>

<cfif waserror IS 1>
	<cfinclude template = "../frm/join.cfm">
<cfelse>
    
<cfset signupdate = now()>
<cfinvoke component="cfcs.common.encryption" method="encryptstring" textstring="#form.password#" returnvariable="enc_password" />
<cfset form.pword = enc_password>
<cfinvoke component="cfcs.common.generator" method="genid" returnvariable="newaid" />
<cfinvoke component="cfcs.common.generator" method="genvstring" returnvariable="form.vstring" />
<cfset form.aid = newaid>
<cfset form.atype = 1>

<cfif form.refid GT 0>
	<cfinvoke component="cfcs.accounts.account" method="getuserbyaid" returnvariable="qparent" />
	<cfif qParent.inherit_type IS 1>
        <cfset form.atype = qParent.atype>
    </cfif>
</cfif>
<!---insert user account--->    
<cfinvoke component="cfcs.common.dbqueries" method="insertdata" tablename="core_accounts" returnvariable="recid" />
<!---excute any plugins--->
<cfif fileexists('#request.basepath#plugins/account_create/oncreate.cfm')>
	<cfinclude template="#request.absolutepath#plugins/account_create/oncreate.cfm">
</cfif>

<!---send email if the files exist--->
<cfif fileexists("#request.basepath#docs/includes/account_emailsubject.cfm") AND fileexists("#request.basepath#docs/includes/account_emailbody.cfm")>
    <cfsavecontent variable="mail_subj">
        <cfinclude template = "/docs/includes/account_emailsubject.cfm">
    </cfsavecontent>
    
    <cfsavecontent variable="mail_body">
        <cfinclude template = "/docs/includes/account_emailbody.cfm">
    </cfsavecontent>
    
    <cfset the_link = "#request.homeurl#index.cfm?c=account_join&a=verify&vstring=#vstring#">

    <cfset mail_subj = replacenocase(mail_subj, "{firstname}", form.firstname, "ALL")>
    <cfset mail_subj = replacenocase(mail_subj, "{email}", form.email, "ALL")>

    <cfset mail_body = replacenocase(mail_body, "{firstname}", form.firstname, "ALL")>    
    <cfset mail_body = replacenocase(mail_body, "{link}", the_link, "ALL")>
    <cfset mail_body = replacenocase(mail_body, "{homeurl}", request.homeurl, "ALL")>
    <cfset mail_body = replacenocase(mail_body, "{vstring}", vstring, "ALL")>
    <cfset mail_body = replacenocase(mail_body, "{email}", form.email, "ALL")>
    <cfset mail_body = replacenocase(mail_body, "{password}", form.password, "ALL")>    

	<!---send out confirmation email--->
    <cfmail from="#request.site_email#" to="#form.email#" subject="#mail_subj#" type="html">
#mail_body#
    </cfmail>
</cfif>

<cflocation url = "index.cfm?c=account_create&a=welcome&aid=#aid#">    
</cfif>